package com.meihua.shellapp.activity.set;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 设置支付密码界面
 */
public class SetPayPwdActiity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;//返回
    @Bind(R.id.tv_title)
    TextView tvTitle;//标题
    @Bind(R.id.show_zhanghao)
    TextView mNumber;//手机号码
    @Bind(R.id.show_new_pwd)
    EditText mnewPwd;
    @Bind(R.id.show_new_confirm_pwd)
    EditText mPwdCon;

    private String phone;
    private Toast toast;

    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_pwd;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        phone = getIntent().getStringExtra("phone");
        tvTitle.setVisibility(View.VISIBLE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("设置支付密码");
        phone = phone.replace(phone.substring(3, 7), "****");
        mNumber.setText(phone);
    }

    @OnClick(R.id.paypwd_submit)
    public void onViewClicked() {
        submit();
    }

    public void showShortCenter(Context context, String content) {

        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(content);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }

    private void submit() {
        if (TextUtils.isEmpty(mnewPwd.getText()) || TextUtils.isEmpty(mnewPwd.getText().toString().trim())) {
            showShortCenter(this, "密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(mPwdCon.getText()) || TextUtils.isEmpty(mPwdCon.getText().toString().trim())) {
            showShortCenter(this, "确认密码不能为空");
            return;
        }

        if (isPwdAvaible(mnewPwd.getText().toString())) {
            showShortCenter(this, "密码不能是重复连续的数字");
            return;
        }
        if (!mnewPwd.getText().toString().trim().equals(mPwdCon.getText().toString().trim())) {
            showShortCenter(this, "两次输入的密码不一致");
            return;
        }

        Map<String, String> params = new HashMap() {{
            put("m_number", MyApplication.getInstance().getUserId());
            put("pay_password", mnewPwd.getText().toString().trim());
        }};
        NetHelper.post(URL.getUrl().SETPAYPASSWORD, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                try {
                    JSONObject object = new JSONObject(response.body());
                    if (object.getInt("code") == 1) {
                        showShortCenter(SetPayPwdActiity.this, object.getString("msg"));
                        SharedPreferencesUtil.getInstance().saveString(SetPayPwdActiity.this, Constant.PAY_PASSWARD, "1");
                        finish();
                    } else {
                        showShortCenter(SetPayPwdActiity.this, "失败，" + object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, false);
    }

    private boolean isPwdAvaible(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length - 1; i++) {
            /*连续的数字*/
          if(Math.abs(String.valueOf(chars[i]).hashCode()-String.valueOf(chars[i+1]).hashCode())==1||String.valueOf(chars[i]).hashCode()==String.valueOf(chars[i+1]).hashCode()){
              return true;
          }else {
              return false;
          }
        }
        return false;
    }

}
